/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package kolsim;

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;

/**
 *
 * @author asage12
 */
public class Statistics {
    Double[] data;
    double size;    

    public Statistics(Double[] data) 
    {
        this.data = data;
        size = data.length;
    }   

    public void printCsv(String filename) throws IOException {
        PrintWriter out = new PrintWriter(new FileWriter(filename), true);
        StringBuilder outStr = new StringBuilder();
        
        for (int i = 0; i < size; i++) {
            outStr.append(data[i]);
            outStr.append(System.getProperty("line.separator"));
        }
        
        out.write(outStr.toString());
        out.close();        
    }
    
    public double getSum() {
        double sum = 0.0;
        for (double a : data) {
            sum += a;
        }
        return sum;        
    }
    
    public double getMean()
    {
        return (getSum() / size);
    }

    public double getVariance()
    {
        double mean = getMean();
        double temp = 0;
        for(double a :data)
            temp += (mean-a)*(mean-a);
            return temp/size;
    }

    public double getStdDev()
    {
        return Math.sqrt(getVariance());
    }

    public double median() 
    {
           double[] b = new double[data.length];
           System.arraycopy(data, 0, b, 0, b.length);
           Arrays.sort(b);

           if (data.length % 2 == 0) 
           {
              return (b[(b.length / 2) - 1] + b[b.length / 2]) / 2.0;
           } 
           else 
           {
              return b[b.length / 2];
           }
    }
}
